using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using LuaInterface;
using UnityEngine.Events;

public class LuaOperation : MonoBehaviour
{
    public event UnityAction AwakeEvent;
    public event UnityAction StartEvent;
    public event UnityAction EnableEvent;
    public event UnityAction DisableEvent;
    public event UnityAction DestroyEvent;
    public event UnityAction UpdateEvent;
    public event UnityAction LateUpdateEvent;

    public LuaTable mLuaTable;
    void Awake()
    {
        //Debug.LogError("Awake");
        if (AwakeEvent != null)
        {
            AwakeEvent();
        }
    }

    void Start()
    {
        //Debug.LogError("Start");
        if (StartEvent != null)
        {
            StartEvent();
        }
    }

    void OnEnable()
    {
        if (EnableEvent != null)
        {
            EnableEvent();
        }
    }

    void OnDisable()
    {
        if (DisableEvent != null)
        {
            DisableEvent();
        }
    }

    void OnDestroy()
    {
        if (DestroyEvent != null)
        {
            try{
                DestroyEvent();
            }
            catch(Exception)
            {
                //Debug.LogError(name);
                // Debug.LogError(e);
            }            
        }
        Clear();
    }

    void Update()
    {
        if (UpdateEvent != null)
        {
            UpdateEvent();
        }
    }

    void LateUpdate()
    {
        if (LateUpdateEvent != null)
        {
            LateUpdateEvent();
        }
    }

    public void Clear()
    {
        AwakeEvent = null;
        StartEvent = null;
        EnableEvent = null;
        DisableEvent = null;
        DestroyEvent = null;
        UpdateEvent = null;
        LateUpdateEvent = null;
        mLuaTable = null;
    }

    void OnApplicationQuit()
    {
        Clear();
    }
}